package com.coder.rental.utils;

import com.coder.rental.service.IFinanceService;
import com.coder.rental.service.IMailService;
import com.coder.rental.vo.MailVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Component
public class SendMailUtil {
    @Resource
    private IFinanceService iFinanceService;
    @Resource
    private IMailService iMailService;
    @Value("${spring.mail.username}")
    private String from;
    @Scheduled(cron = "0 0 20 ? * *")// 每晚8点发送邮件
    //@Scheduled(cron = "*/10 * * * * ?")
    public void sendMail() {
        StringBuffer content = new StringBuffer();
        content.append("今日收入：")
                .append("租金收入：")
                .append(iFinanceService.sumRentPay().getCountRentActual())
                .append("，押金收入：")
                .append(iFinanceService.sumDeposit());
        MailVo mailVo = new MailVo();
        mailVo.setFrom(from);
        mailVo.setTo("3528109435@qq.com");
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        mailVo.setSubject(date+"日收入统计");
        mailVo.setContent(content.toString());
        iMailService.sendMail(mailVo);
    }

}
